{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. 参数传递：值传递和引用传递"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.1 python变量和赋值基本原理\n",
    "\n",
    "- 变量的赋值，只是表示让变量指向了某个对象，并不表示拷贝对象给变量；而一个对象，可以被多个变量所指向。\n",
    "- 可变对象（列表，字典，集合等等）的改变，会影响所有指向该对象的变量。\n",
    "- 对于不可变对象（字符串、整型、元组等等），所有指向该对象的变量的值总是一样的，也不会改变。但是通过某些操作（+= 等等）更新不可变对象的值时，会返回一个新的对象。\n",
    "- 变量可以被删除，但是对象无法被删除。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.2 python 函数的参数传递\n",
    "\n",
    "- Python 的参数传递是赋值传递 （pass by assignment），或者叫作对象的引用传递（pass by object reference）。Python 里所有的数据类型都是对象，所以参数传递时，只是让新变量与原变量指向相同的对象而已，并不存在值传递或是引用传递一说。\n",
    "- 可变对象\n",
    "- 不可变对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b:1,id:140726817843640\n",
      "a:1,b:2,id:140726817843672\n",
      "a:1,id:140726817843640\n"
     ]
    }
   ],
   "source": [
    "def my_func2(b):\n",
    "  print(\"b:{},id:{}\".format(b,id(b)))\n",
    "  b = 2\n",
    "  print(\"a:{},b:{},id:{}\".format(a,b,id(b)))\n",
    "\n",
    "a = 1\n",
    "my_func2(a)\n",
    "print(\"a:{},id:{}\".format(a,id(a)))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 可变对象\n",
    "def my_func3(l2):\n",
    "  l2.append(4)\n",
    "\n",
    "l1 = [1, 2, 3]\n",
    "my_func3(l1)\n",
    "l1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def my_func4(l2):\n",
    "  l2 = l2 + [4]\n",
    "\n",
    "l1 = [1, 2, 3]\n",
    "my_func4(l1)\n",
    "l1\n",
    "#  l2 = l2 + [4]，表示创建了一个“末尾加入元素 4“的新列表，并让 l2 指向这个新的对象。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.3 总结\n",
    "\n",
    "- Python 中参数的传递既不是值传递，也不是引用传递，而是赋值传递，或者是叫对象的引用传递。\n",
    "- 这里的赋值或对象的引用传递，不是指向一个具体的内存地址，而是指向一个具体的对象。\n",
    "- 如果对象是可变的，当其改变时，所有指向这个对象的变量都会改变\n",
    "- 如果对象不可变，简单的赋值只能改变其中一个变量的值，其余变量则不受影响。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n",
      "True\n",
      "2971102083968 2971102075328 2971102075328\n"
     ]
    }
   ],
   "source": [
    "l1 = [1, 2, 3]\n",
    "l2 = [1, 2, 3]\n",
    "l3 = l2\n",
    "print(l1 == l2)\n",
    "print(l1 is l2)\n",
    "print(l2 is l3)\n",
    "print(id(l1),id(l2),id(l3))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'a': 10, 'b': 20}\n"
     ]
    }
   ],
   "source": [
    "def func(d):\n",
    "    d['a'] = 10\n",
    "    d['b'] = 20\n",
    "\n",
    "d = {'a': 1, 'b': 2}\n",
    "func(d)\n",
    "print(d)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
